package com.baoli.umpush.service.impl;

import com.baoli.umpush.pojo.MsgBody;
import com.baoli.umpush.service.UmengService;
import com.baoli.umpush.util.AndroidUmengUtils;
import com.baoli.umpush.util.UmengUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UmengServiceImpl implements UmengService {

    @Autowired
    private UmengUtil umengUtil;

    @Value("${umeng.android.appkey}")
    private String androidAppkey;

    @Value("${umeng.android.appMasterSecret}")
    private String androidApMasterSecret;

    @Value("${umeng.IOS.appkey}")
    private String iosAppkey;

    @Value("${umeng.IOS.appMasterSecret}")
    private String iosApMasterSecret;

    @Value("${umeng.activemq.retry.failTimes}")
    private Long mqRetryTimes;

    @Value("${umeng.activemq.retry.time}")
    private Integer mqRetryTime;

    @Value("${umeng.activemq.failurequeue}")
    private String failureQueueName;

    @Value("${mail.level}")
    private int emailNotifyLevel;

    /**
     * 推送消息
     */
    @Override
    public void push(MsgBody umengMsg) throws Exception {

        if (umengMsg.getAndroidUsers() != null) {
            this.umengAndroidSend(umengMsg);
        }
        if (umengMsg.getIosUsers() != null) {
//            this.umengIOSSend(umengMsg);
        }

    }

    /**
     * 安卓推送
     */
    private void umengAndroidSend(MsgBody umengMsg) throws Exception {

        int num = umengMsg.getAndroidUsers().size();
        String result = null;
        AndroidUmengUtils androidUtils = new AndroidUmengUtils(androidAppkey, androidApMasterSecret);

        if (num == 0) {
            // 广播，限制每天发送10次，超出次数第二天凌晨发送
            long time = umengUtil.broadcastNextPushTime();
            if (time > 0) {
                // TODO 可以实现明天推送的代码，比如消息队列，定时器等等
                log.warn("Umeng android send broadcast over the number of times,try again tomorrow,infomation is {}",
                        umengMsg);
            } else {
                result = androidUtils.sendAndroidBroadcast(umengMsg);
            }
        } else if (num == 1) {
            // 单播，无发送限制
            result = androidUtils.sendAndroidUnicast(umengMsg, umengMsg.getAndroidUsers().get(0));
        } else if (num < 500) {
            // 列播，无发送限制
            result = androidUtils.sendAndroidListcast(umengMsg, StringUtils.join(umengMsg.getAndroidUsers(), ","));
        } else {
            // 文件播
            long time = umengUtil.filecastNextPushTime();
            if (time > 0) {
                // TODO 可以实现下次推送的代码，比如消息队列，定时器等等
                log.warn(
                        "Umeng android send filecast over the number of times,try again {} millisecond,infomation is {}",
                        time,
                        umengMsg);
            } else {
//                result = androidUtils.sendAndroidFilecast(umengMsg, StringUtils.join(umengMsg.getAndroidUsers(), "\n"));
            }
        }

        if (StringUtils.isNotBlank(result)) {
            // TODO 由友盟返回的错误信息
            log.error("Umeng android send failure,the error message is {}", result);
        }

    }

    /**
     * ios推送
     */
    /*
    private void umengIOSSend(MsgBody umengMsg) throws Exception {
        IosUmengUtils iosUtils = new IosUmengUtils(iosAppkey, iosApMasterSecret);
        String result = null;
        int num = umengMsg.getIosUsers().size();
        if (num == 0) {
            // 广播，限制每天发送10次，超出次数第二天凌晨发送
            long time = umengUtil.broadcastNextPushTime();
            if (time > 0) {

                log.warn("Umeng IOS send broadcast over the number of times,try again tomorrow,infomation is {}",
                        umengMsg);
            } else {
                result = iosUtils.sendIOSBroadcast(umengMsg);
            }
        } else if (num == 1) {
            // 单播，无发送限制
            result = iosUtils.sendIOSUnicast(umengMsg, umengMsg.getIosUsers().get(0));
        } else if (num < 500) {
            // 列播，无发送限制
            result = iosUtils.sendIOSListcast(umengMsg, StringUtils.join(umengMsg.getIosUsers(), ","));
        } else {
            // 文件播
            long time = umengUtil.filecastNextPushTime();
            if (time > 0) {

                log.warn("Umeng IOS send filecast over the number of times,try again {} millisecond,infomation is {}",
                        time,
                        umengMsg);
            } else {
                result = iosUtils.sendIOSFilecast(umengMsg, StringUtils.join(umengMsg.getIosUsers(), "\n"));
            }
        }
        if (StringUtils.isNotBlank(result)) {
            log.error("Umeng IOS send failure,the error message is {}", result);
        }
    }*/

}

